<script>
	function dateX(hijCal, cal,alerts) {
		if ($("#hyear").val() != "" && $("#hmonth").val() != "" && $("#hday").val() != "") {
			var hyear = parseInt($('#hyear').val(), 10);
			var hmonth = parseInt($('#hmonth').val(), 10);
			var hday = parseInt($('#hday').val(), 10);

			if (!hijCal.isValid(hyear, hmonth, hday)) {
				if (alerts == 1) {
					alert("تاريخ الإنتهاء الهجري غير صحيح");
				}
				$('#hyear').val("");
				$('#hmonth').val("");
				$('#hday').val("");
				$('#expiry_date').val(mdate);
			} else {
				//Get today's date
				var htoday = hijCal.today();
				//Input as date
				var hdate = hijCal.newDate(hyear, hmonth, hday);
				var mdate = cal.fromJD(hdate.toJD());
				//pass to correct locations :)
				$('#hijridate').val(hdate);
				$('#expiry_date').val(mdate);
				//Alert if the ID expired
				if (hdate.toJD() <= htoday.toJD()) {
					if (alerts == 1) {
						alert("ملحوظة: الإثبات منتهي");
					}
				}

			}
		}
	}

	$(document).ready(function() {
		var cal = $.calendars.instance();
		var hijCal = $.calendars.instance("ummalqura");
		$(".add-button-no-submit").click(function() {
			dateX(hijCal, cal,1);
		});

		//Fill Expiry Date on Change after making sure all fields are filled.
		$(document).on('change',"#hyear",function() {
			dateX(hijCal, cal,1);
		});
		$(document).on('change',"#hmonth",function() {
			dateX(hijCal, cal,1);
		});
		$(document).on('change',"#hday",function() {
			dateX(hijCal, cal,1);
		});
	}); 
</script>
<h1 class="right">ادارة العملاء </h1><!--<div class="h1-dot"></div>-->
<a href="#add_client" class="add_client_pop">
  <img src="images/add-icon.png" align="left" class="left" alt=""/>
</a>
<div id="add_client" class="zoom-anim-dialog mfp-hide">
<?php require("Customers/Create.php"); ?>
</div>

<a href="#add_company" class="add-pop company_add">
  <img src="images/add-company-icon.png" align="left" class="left" alt=""/>
</a>
<div id="add_company" class="zoom-anim-dialog mfp-hide pop-default">
<?php require("Customers/Companies/Create.php"); ?>
</div>

<span class="left grey medium margin-left11 margin-top5"><?php echo date('g:ia d/m/Y')?></span>
<div class="clear"></div>
<div id="dashboard-container">
	<div class="bookings-filter">
    	<table class="bookings-filter">
        <thead>
        <tr>
          
          <form name="searchClient" action="customers.php" method="get" id="searchClient">
            <td>رقم الإثبات  &nbsp;:&nbsp;<input type="text" class="small-input identify" name="id" placeholder="" value="<?php if(isset($binds["id"])) echo $binds["id"]; else ""; ?>" /></td>
              <td>الاسم&nbsp;:&nbsp;<input type="text" class="small-input" name="name" value="<?php if(isset($binds["name"])) echo $binds["name"]; else ""; ?>"/></td>
              <td>جوال&nbsp;:&nbsp;<input type="text" class="small-input" name="mobile" value="<?php if(isset($binds["mobile"])) echo $binds["mobile"]; else "" ?>" /></td>
               <td>
              
              <td style="width:75px;"><a href="#"><div class="search-botton">بحث</div></a></td>
              <td style="width:75px;"><!--<a href="../public/export.php"><div class="export-button">تصدير</div></a>--></td>
              
              <input class="company-select" name="check" type="hidden" value="<?php if(isset($check) ) {echo $check;} ?>"/>
            </tr>
          </form>
          </thead>
      </table>
      
    </div>
    <form name="getCompanies" action="customers.php" method="GET" id="getCompanies">
               <?php if($check == 1) { ?>
               <input type="checkbox" checked='checked' class="company-select" name="check"  />
               <?php }else { ?>
                <input class="company-select" name="check" type="checkbox" />
                <?php } ?>
               &nbsp;الشركات</td>
               </form>
    
    <div class="dashboard-row">
    
    	<table class="customers-table">
        <thead>
        	<?php
          //if Checkbox is on, view customers table
          if($check != 1) { ?>
          <tr class="customers-table-head">
          	
            	<td width="160px">الاسم</td>
                <td width="140px">نوع الإثبات</td>
                <td width="100px">رقم الإثبات</td>
                <td width="110px">مكان الاصدار</td>
                <td width="100px">جوال</td>
                <td width="160px">البريد الالكتروني</td>
                <td width="100px">العمليات</td>
                <!--<td>الاسم</td>
                <td>هاتف</td>
                <td>المدينة</td>
                <td>الشخص المسؤول </td>
                <td>البريد الالكتروني </td>
                <td>الخصم</td>
                <td>العمليات</td>!-->
            </tr>
            </thead>
            
                <?php if(!empty($clients)) { ?>
                <?php foreach($clients as $client) {?>
                
                <tr class="cell-parent">
                    <td ><?php echo $client['name']; ?> </td>
                    <td ><?php echo $client['identification_type']; ?>  </td>
                    <td ><?php echo $client['identification_number'] ;?></td>
                    <td ><?php echo $client['publication_place']; ?></td>
                    <td ><?php echo $client['mobile'] ;?></td>
                    <td ><?php echo $client['email']; ?></td>
                    <td>
                      <!--  View Pop-up  -->
                      <a href="view-pop-ups/client.php?id=<?php echo $client['id'] ?>" class="ajax-pop-ups-magnific-no-modal">
                        <img src="images/view.png" class="" align="" alt="View" title="عرض العميل"/>
                      </a>   
                      
                      
                      <!--  Edit Pop-up  -->
                      <a href="edit-pop-ups/client.php?id=<?php echo $client['id'] ?>" class="ajax-pop-ups-magnific-no-modal">
                        <img src="images/edit.png" class="" align="" alt="View" title="تعديل العميل"/>
                      </a>   
<?php
					  if ($_SESSION['r']<=1)
					  {
?>
                      <a href="#" class="delete"><img src="images/x.png" class="margin-right5" align="" alt="Delete"/></a>
                        <input class="delete-id" type="hidden" value="<?php echo $client['id'] ?>"/>
                        <input class="filter-id" type="hidden" value="2"/> <!-- 2 means delete customer -->
						
<?php
}
?>						
                    </td>
                </tr>
                <?php  }
                }else{
                  echo "<td colspan='7' class='red large' align=''>لا توجد نتائج</td>";
                } ?>
              
            <?php }else{ ?>
                      <tr class="customers-table-head">
            	<td width="160px">الاسم</td>
                <td width="140px">الهاتف</td>
                <td width="100px">المدينة</td>
                <td width="160px">الشخص المسئول</td>
                <td width="160px">البريد الالكتروني</td>
                <td width="70px">الخصم</td>
                <td width="100px">العمليات</td>
                <!--<td>الاسم</td>
                <td>هاتف</td>
                <td>المدينة</td>
                <td>الشخص المسؤول </td>
                <td>البريد الالكتروني </td>
                <td>الخصم</td>
                <td>العمليات</td>!-->
            </tr>
            
                <?php if(!empty($clients)) { ?>
                <?php foreach($clients as $client) {?>
                <?php $client_name = query("SELECT name from clients where id=?",$client['representative_id']); ?> 
                <tr class="cell-parent">
                    <td ><?php echo $client['company_name']; ?> </td>
                    <td ><?php echo $client['company_phone']; ?>  </td>
                    <td ><?php echo $client['city'] ;?></td>
                    <td ><?php if(isset($client_name[0]['name']))echo $client_name[0]['name']; ?></td>
                    <td ><?php echo $client['company_email'] ;?></td>
                    <td ><?php echo $client['discount_for_company']; ?></td>
                    <td class="white-bg">
                      <!--  View Pop-up  -->
                      <a href="view-pop-ups/company.php?id=<?php echo $client['company_id'] ?>" class="ajax-pop-ups-magnific-no-modal"><img src="images/view.png" class="" align="" alt="View"/></a>
                                            
                      <!--  Edit Pop-up  -->
                       <a href="edit-pop-ups/company.php?id=<?php echo $client['company_id'] ?>" class="ajax-pop-ups-magnific-no-modal">
                        <img src="images/edit.png" class="" align="" alt="View" title="تعديل الشركة"/>
                      </a>   
                                            <?php
					  //if not admin or ceo
					  if ($_SESSION['r']<=1)
					  {
					  ?>
                        <a href="#" class="delete"><img src="images/x.png" class="margin-right5" align="" alt="Delete"/></a>
                        <input class="delete-id" type="hidden" value="<?php echo $client['company_id'] ?>"/>
                        <input class="filter-id" type="hidden" value="4"/> <!-- 4 means delete company -->
												<?php
						}
						?>
                    </td>
                </tr>
                <?php  }
                }else{
                  echo "<td colspan='7' class='red large' align=''>لا توجد نتائج</td>";
                } ?>

            <?php } ?>
        </table>
    </div>
            <?php
          $start = $page * 20 -19;
          $end  = $start + 19;
          if($end > $totalPages)
            $end = $totalPages
          
        ?>
        <?php if(!empty($clients)) { ?>
        <div class="clear"></div>
        <div class="relative">
          <span class="middle xsmall showing-results absolute">عرض النتائج <?php echo $start; ?> - <?php echo $end; ?> من إجمالى <?php echo $totalPages; ?></span>
          <div id="page-selection"></div>
        </div>
        <div class="hidden totalPages"><?php if(isset($totalPages)) {echo $totalPages;} ?></div>
        <div class="hidden currentPage"><?php if(isset($page)) {echo $page;} ?></div>
        <?php } ?>
               


<div class="clear"></div>
</div>